#include"namestorage.h"

NameStorage::NameStorage() {
}

NameStorage::NameStorage(NameStorage const & ns) : storage(ns.storage) {
}

NameStorage::~NameStorage() {
}

string const & NameStorage::operator[](int i) const {
	return storage[i];
}

string & NameStorage::operator[](int i) {
	return storage[i];
}

int NameStorage::findName(string const & s) const {
	bool stop = false;
	unsigned i = 0;
	for(i = 0; !stop && (i < storage.size()); i++)
		stop = (s.compare(storage[i]) == 0);
	return stop ? (i - 1) : -1;
}

int NameStorage::addName(string const & s) {
	int i = findName(s);
	if (i == -1) {
		storage.push_back(s);
		return storage.size() - 1;
	}
	else
		return i;
}

int NameStorage::addOrRename(string const & s) {
	int i = findName(s);
	if (i == -1) {
		storage.push_back(s);
	}
	else {
		string newname = s;
		do {
			newname += "1";
		} while (findName(newname) != -1);
		storage.push_back(newname);
	}
	return (storage.size() - 1);
}
